﻿package com.gaotime.app.kernel.common.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Service;

import com.gaotime.app.kernel.common.constants.Constants;

/**
 * 获取系统配置信息公共处理类
 * @author javach
 * @mail javach@foxmail.com
 * @version v1.0
 */
@Service
public class ConfigurationHelper {

	private static final Log log = LogFactory.getLog(ConfigurationHelper.class);

	@Autowired
	private ResourceBundleMessageSource messageSource;

	public String getProperty(String key) {
		return getProperty(key, null);
	}

	public String getProperty(String key, Object[] arg) {
		try {
			String msg = messageSource.getMessage(key, arg, Constants.DEFAULT_LOCALE);
			return msg != null ? msg.trim() : msg;
		} catch (NoSuchMessageException e) {
			log.warn("Message of key " + key + " NOT found!");
			return key;
		}
	}

	public String getPropertyDefault(String key, String defaultValue) {
		return getPropertyDefault(key, null, defaultValue);
	}

	public String getPropertyDefault(String key, Object[] arg, String defaultValue) {
		try {
			String msg = messageSource.getMessage(key, arg, Constants.DEFAULT_LOCALE);
			return msg != null ? msg.trim() : msg;
		} catch (NoSuchMessageException e) {
			return defaultValue;
		}
	}

	public int getIntProperty(String key, int defaultValue) {
		return this.getIntProperty(key, null, defaultValue);
	}

	public int getIntProperty(String key, Object[] arg, int defaultValue) {
		try {
			String s = messageSource.getMessage(key, arg, Constants.DEFAULT_LOCALE);
			s = (s != null ? s.trim() : s);
			return Integer.parseInt(s);
		} catch (NoSuchMessageException e) {
			return defaultValue;
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	public long getLongProperty(String key, long defaultValue) {
		return getLongProperty(key, null, defaultValue);
	}

	public long getLongProperty(String key, Object[] arg, long defaultValue) {
		try {
			String value = messageSource.getMessage(key, arg, Constants.DEFAULT_LOCALE);
			value = (value != null ? value.trim() : value);
			return Long.parseLong(value);
		} catch (NoSuchMessageException e) {
			return defaultValue;
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}

	public boolean getBooleanProperty(String key, boolean defaultValue) {
		return getBooleanProperty(key, null, defaultValue);
	}

	public boolean getBooleanProperty(String key, Object[] arg, boolean defaultValue) {
		boolean result = defaultValue;
		try {
			String value = messageSource.getMessage(key, arg, Constants.DEFAULT_LOCALE);
			value = (value != null ? value.trim() : value);
			if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
				result = Boolean.parseBoolean(value);
			}
			return result;
		} catch (NoSuchMessageException e) {
			return result;
		} catch (NumberFormatException e) {
			return result;
		}
	}
}